home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / bisonpcb / derives.c < prev    next >
C/C++ Source or Header  |  1987-02-12  |  3KB  |  120 lines

  1. /* Match rules with nonterminals for bison,
  2.    Copyright (C) 1984 Bob Corbett and Free Software Foundation, Inc.
  3.   
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.   
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.   
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /* set_derives finds, for each variable (nonterminal), which rules can derive it.
  22.    It sets up the value of derives so that
  23.    derives[i - ntokens] points to a vector of rule numbers, terminated with a zero.  */
  24.  
  25. /*
  26.  * Port to PC by Whit Gregg
  27.  *               Nourse, Gregg & Browne, Inc.
  28.  *         1 Horizon Road
  29.  *         Fort Lee, NJ  07024
  30.  */
  31.  
  32.  
  33. #include <stdio.h>
  34. #include <malloc.h>
  35. #include "new.h"
  36. #include "types.h"
  37. #include "gram.h"
  38. #include "func.h"
  39.  
  40.  
  41.     short **derives;
  42.  
  43.  
  44. void 
  45. set_derives()
  46. {                /* WG */
  47.     register int i;
  48.     register int lhs;
  49.     register shorts *p;
  50.     register short *q;
  51.     register shorts **dset;
  52.     register shorts *delts;
  53.  
  54.     dset = NEW2(nvars, shorts *) - ntokens;
  55.     delts = NEW2(nrules + 1, shorts);
  56.  
  57.     p = delts;
  58.     for (i = nrules; i > 0; i--) {
  59.         lhs = rlhs[i];
  60.         p->next = dset[lhs];
  61.         p->value = i;
  62.         dset[lhs] = p;
  63.         p++;
  64.         }
  65.  
  66.     derives = NEW2(nvars, short *) -ntokens;
  67.     q = NEW2(nvars + nrules, short);
  68.  
  69.     for (i = ntokens; i < nsyms; i++) {
  70.         derives[i] = q;
  71.         p = dset[i];
  72.         while (p) {
  73.             *q++ = p->value;
  74.             p = p->next;
  75.             }
  76.         *q++ = -1;
  77.         }
  78.  
  79. #ifdef    DEBUG
  80.     print_derives();
  81. #endif
  82.  
  83.     FREE(dset + ntokens);
  84.     FREE(delts);
  85.     }
  86.  
  87.  
  88. void 
  89. free_derives()
  90. {                /* WG */
  91.     FREE(derives[ntokens]);
  92.     FREE(derives + ntokens);
  93.     }
  94.  
  95.  
  96.  
  97. #ifdef    DEBUG
  98.  
  99. print_derives()
  100. {
  101.     register int i;
  102.     register short *sp;
  103.  
  104.     extern char **tags;
  105.  
  106.     printf("\n\n\nDERIVES\n\n");
  107.  
  108.     for (i = ntokens; i < nsyms; i++) {
  109.         printf("%s derives", tags[i]);
  110.         for (sp = derives[i]; *sp > 0; sp++) {
  111.             printf("  %d", *sp);
  112.             }
  113.         putchar('\n');
  114.         }
  115.  
  116.     putchar('\n');
  117.     }
  118.  
  119. #endif
  120.